package org.yscz.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.yscz.common.HttpStatus;
import org.yscz.common.Result;
import org.yscz.entity.Report;
import org.yscz.entity.vo.RecordVo;
import org.yscz.mapper.ReportMapper;
import org.yscz.service.RecordService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 测评记录接口
 *
 * @author muqs
 * @create 2021-08-31 22:45
 */
@RestController
@Slf4j
@Api("测评记录接口")
public class RecordController {

    @Autowired
    private RecordService recordService;

    @Resource
    private ReportMapper reportMapper;

    /**
     * 删除测评记录接口
     *
     * @param recordId 前台传递的测评记录数据
     * @return Result对象
     */
    @DeleteMapping("/record/del/{recordId}")
    @ApiOperation(value = "删除测评记录接口", httpMethod = "DELETE")
    public Result del(@PathVariable Long recordId) {
        log.info("recordId:[{}]", recordId);
        recordService.del(recordId);
        return Result.success(HttpStatus.SUCCESS, "删除测评记录成功", "");
    }

    /**
     * 查询测评记录列表接口
     *
     * @param recordTesteeName 被测人姓名
     * @param sex              性别
     * @param testeeGroupId    班级ID
     * @param scaleName        量表名称
     * @param startTime        开始时间
     * @param endTime          结束时间
     * @param pageNum          当前页
     * @param pageSize         每页数据条数
     * @return Result对象
     */
    @GetMapping("/record/listPage")
    @ApiOperation(value = "查询测评记录接口", httpMethod = "GET")
    public Result listPage(@RequestHeader(value = "userId", required = false) Long userId,
                           @RequestParam(value = "recordTesteeName", required = false) String recordTesteeName,
                           @RequestParam(value = "sex", required = false) Integer sex,
                           @RequestParam(value = "testeeGroupId", required = false) Long testeeGroupId,
                           @RequestParam(value = "scaleName", required = false) String scaleName,
                           @RequestParam(value = "startTime", required = false) String startTime,
                           @RequestParam(value = "endTime", required = false) String endTime,
                           @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "6") Integer pageSize) {
        JSONObject reusltObj = new JSONObject();
        Map condition = new HashMap();
        IPage<RecordVo> page = new Page(pageNum, pageSize);

        if (!StringUtils.isEmpty(recordTesteeName)) {
            condition.put("recordTesteeName", "%" + recordTesteeName.replaceAll(" ", "") + "%");
        }

        if (sex != null) {
            condition.put("sex", sex);
        }
        if (testeeGroupId != null) {
            condition.put("testeeGroupId", testeeGroupId);
        }

        if (!StringUtils.isEmpty(scaleName)) {
            condition.put("scaleName", "%" + scaleName.replaceAll(" ", "") + "%");
        }

        if (!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)) {
            condition.put("startTime", startTime + " 00:00:00");
            condition.put("endTime", endTime + " 23:59:59");
        }

        condition.put("userId", userId);
        IPage<RecordVo> recordPage = recordService.listPageByCondition(page, condition);

        reusltObj.put("list", recordPage == null ? new ArrayList<>() : recordPage.getRecords());
        reusltObj.put("total", recordPage == null ? 0 : recordPage.getTotal());

        return Result.success(HttpStatus.SUCCESS, "查询测评记录列表成功", reusltObj);
    }

    /**
     * 查看测评报告接口
     *
     * @param recordId 测评记录
     * @return Result对象
     */
    @GetMapping("/report/reportDetail/{recordId}")
    @ApiOperation(value = "查看测评报告接口", httpMethod = "GET")
    public Result reportDetail(@PathVariable Long recordId) {
        return Result.success(HttpStatus.SUCCESS, "查看测评报告成功", recordService.reportDetail(recordId));
    }

    /**
     * 修改测评报告结论
     *
     * @param report 测评记录
     * @return Result对象
     */
    @PostMapping("/report/modify")
    @ApiOperation(value = "查看测评报告接口", httpMethod = "POST")
    public Result reportModify(@RequestBody Report report) {
        reportMapper.updateById(report);
        return Result.success(HttpStatus.SUCCESS, "修改测评报告结论成功", "");
    }
}
